package com.voice.core.api.utils;



import com.voice.core.api.model.ApiRequest;

import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;

public class ApiRequestMaker {
    public ApiRequestMaker() {
    }

    public static void make(ApiRequest request, String appKey, String appSecret) {
        request.setPath(combinePathParam(request.getPath(), request.getPathParams()));
        StringBuilder url = (new StringBuilder()).append(request.getScheme().getValue()).append(request.getHost()).append(request.getPath());
        if (null != request.getQuerys() && request.getQuerys().size() > 0) {
            url.append("?").append(HttpCommonUtil.buildParamString(request.getQuerys()));
        }

        request.setUrl(url.toString());
        Date current = new Date();
        if (null == request.getFirstHeaderValue("date")) {
            request.addHeader("date", getHttpDateHeaderValue(current));
        }

        request.addHeader("x-ca-timestamp", String.valueOf(current.getTime()));
        request.addHeader("host", request.getHost());
        request.addHeader("x-ca-key", appKey);
        if (request.getFirstHeaderValue("content-type")==null) {
            request.addHeader("content-type", request.getMethod().getRequestContentType());
        }

        if (request.getFirstHeaderValue("accept")==null) {
            request.addHeader("accept", request.getMethod().getAcceptContentType());
        }

        if (null != request.getBody() && request.getBody().length > 0) {
            request.addHeader("content-md5", SignUtil.base64AndMD5(request.getBody()));
        }

        String signature = SignUtil.sign(request, appSecret);
        request.addHeader("x-ca-signature", signature);

        String key;
        List values;
        Iterator iterator = request.getHeaders().keySet().iterator();
        while (iterator.hasNext()){
            key = (String)iterator.next();
            values =request.getHeaders().get(key);
            if (null != values && values.size() > 0) {
                for(int i = 0; i < values.size(); ++i) {
                    byte[] temp = ((String)values.get(i)).getBytes(Charset.forName("UTF-8"));
                    values.set(i, new String(temp, Charset.forName("ISO-8859-1")));
                }
            }
            request.getHeaders().put(key, values);
        }
    }

    private static String combinePathParam(String path, Map<String, String> pathParams) {
        if (pathParams == null) {
            return path;
        } else {
            String key;
            for(Iterator var2 = pathParams.keySet().iterator(); var2.hasNext(); path = path.replace("[" + key + "]", (CharSequence)pathParams.get(key))) {
                key = (String)var2.next();
            }

            return path;
        }
    }

    private static String getHttpDateHeaderValue(Date date) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
        dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
        return dateFormat.format(date);
    }
}
